package com.rc.saas.mini.controller;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.common.config.UpYunConfig;
import com.rc.saas.mini.common.constant.SessionConstants;
import com.rc.saas.mini.common.constant.WebConstant;
import com.rc.saas.mini.model.help.UserGuide;
import com.rc.saas.mini.model.tenant.TenantBanner;
import com.rc.saas.mini.model.tenant.TenantBannerExample;
import com.rc.saas.mini.service.help.UserGuideService;
import com.rc.saas.mini.service.tenant.TenantBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 首页
 * Created by Sven on 2021/12/02
 */
@Controller
@Api(tags = "09.首页")
@RequestMapping("/index")
public class IndexController extends MinipBaseController {
    private static Logger logger = LogManager.getLogger(IndexController.class);

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private UserGuideService userGuideService;

    @Autowired
    private TenantBannerService tenantBannerService;

    //@ApiIgnore
    //@RequestMapping(value = "/home", method = RequestMethod.GET)
    //public DataTResponse home(@PathVariable String appid) throws WeixinException {
    //    logger.info("home.appid:{}", appid);
    //
    //    return DataTResponse.SUCCESS();
    //}


    @ResponseBody
    @RequestMapping(value = "/open/listUserGuide", method = RequestMethod.GET)
    @ApiOperation(value = "02.用户指南", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<List<UserGuide>> listUserGuide(HttpServletRequest request) {
        try {
            String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
            //一级菜单
            List<UserGuide> userGuides = userGuideService.selectFirstByTenantCode(tenantCode);
            //二级菜单
            if (userGuides != null && userGuides.size() > 0) {
                for (UserGuide userGuide : userGuides) {
                    List<UserGuide> userGuideSecondList = userGuideService.selectSecondByFirst(userGuide.getUserGuideId(), tenantCode);
                    userGuide.setUserGuides(userGuideSecondList);
                }
            }

            //DataTResponse DataTResponse = DataTResponse.SUCCESS();
            //DataTResponse.put(userGuides);
            return DataTResponse.SUCCESS_DATA(userGuides);
        } catch (Exception e) {
            logger.error("用户指南获取失败！", e);
            return DataTResponse.FAIL("用户指南获取失败！");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/open/listBanner", method = RequestMethod.POST)
    @ApiOperation(value = "01.首页.图片相关(轮播图广告图等等)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bannerType", paramType = "query", value = "轮播图类型(0:公众号;1:APP;2,小程序)默认2", required = true),
            @ApiImplicitParam(name = "imgType", paramType = "query", value = "图片类型(0:启动页,1:app轮播图,2:广告图,3:弹框广告,4公众号轮播图,5,小程序轮播图,6,小程序首页弹窗广告图,7,小程序首页微信官方广告图)默认5", required = true),
    })
    public DataTResponse<List<TenantBanner>> listBanner(HttpServletRequest request,
                                                        @RequestParam(value = "bannerType", defaultValue = "2") String bannerType,
                                                        @RequestParam(value = "imgType", defaultValue = "5") String imgType) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }

        logger.info("getWXBanner.tenantCode:{}:", tenantCode);
        //List<TenantBanner> tenantBannerList = tenantBannerService.selectByTenantCode(tenantCode);
        TenantBannerExample example = new TenantBannerExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode)
                .andBannerTypeEqualTo(bannerType).andImgTypeEqualTo(imgType)
                .andStatusEqualTo(WebConstant.CON1);
        example.setOrderByClause(" created_time desc");
        List<TenantBanner> tenantBanners = tenantBannerService.selectByExample(example);
        return DataTResponse.SUCCESS_DATA(tenantBanners);
    }




}
